import static org.junit.Assert.*;

import omero.ServerError;
import omero.client;
import omero.api.ServiceFactoryPrx;
import omero.api.delete.DeleteCommand;
import omero.api.delete.DeleteHandlePrx;
import omero.model.Image;
import omero.model.ImageI;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import cli.OmeroServer;

import pojos.ImageData;


public class Images {

	
	private static client unsecureClient = null;
	private static ServiceFactoryPrx session = null;
	private static ImageData imageNew;
	
	private static String hostName = "192.168.20.108";
	private static String username = "root";
	private static String password = "omero";
	private static int port  = 10000;
	
	
	@BeforeClass
	public static  void oneTimeSetUp() throws Exception {
		OmeroServer.open(hostName, port, username, password);
	}

	@AfterClass
	public static void oneTimeTearDown() throws Exception {
		OmeroServer.close();
	}

	@Test
	public void testCreateImage() throws ServerError {
		ServiceFactoryPrx session = OmeroServer.getSession();
		
		Image img = new ImageI();
		img.setName(omero.rtypes.rstring("image1"));
		img.setDescription(omero.rtypes.rstring("descriptionImage1"));
		img.setAcquisitionDate(omero.rtypes.rtime(1000000));
		img = (Image) session.getUpdateService().saveAndReturnObject(img);
		long imgId = img.getId().getValue();
		assertNotNull( img );
		//get the new image
		imageNew = OmeroServer.getImage(img.getId().getValue() );
		long imageId = imageNew.getId();
		System.out.println("new id " + imageNew.getId() );
		assertEquals( imgId, imageId );	
	}
	
	@Test
	public void testDeleteImage()  {
		try { 
			ServiceFactoryPrx session = OmeroServer.getSession();
			System.out.println( "image to delete: " + imageNew.getId() );
			DeleteCommand[] cmds = new DeleteCommand[1];
			cmds[0] = new DeleteCommand( "Image", imageNew.getId(), null );
			DeleteHandlePrx handle = session.getDeleteService().queueDelete(cmds);
			assertEquals( handle.errors(), 0 );
			handle.close();
			
		} catch ( Exception e ) {
			System.out.println( "Error: " + e.toString()  );
			e.printStackTrace();
		}
	}

}
